package com.example.test.dialogfactory;

import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.RecyclerView.ViewHolder;
import com.example.test.dialogfactory.MainActivity81.ChooseDialog.CallBack;
import easier.app.x.view.FocusTextWatcher;
import easier.app.x.view.FocusTextWatcher.ClearFocusTextWatcher;
import easier.recycler.BaseViewHolder;
import easier.recycler.MultiTypeAdapter;
import easier.recycler.TypeViewHolder;
import easier.window.dialog.BaseDialogFragment;
import java.util.ArrayList;
import java.util.List;

public class MainActivity81 extends AppCompatActivity {

    private Button mClick;

    public static void start( Context context ) {

        Intent starter = new Intent( context, MainActivity81.class );
        context.startActivity( starter );
    }

    @Override
    protected void onCreate( Bundle savedInstanceState ) {

        super.onCreate( savedInstanceState );
        setContentView( R.layout.activity_main81 );
        initView();
    }

    private void initView() {

        mClick = (Button) findViewById( R.id.click );
        mClick.setOnClickListener( new OnClickListener() {
            @Override
            public void onClick( View v ) {

                ChooseDialog.newInstance( "requestName", "", "", new CallBack() {
                    @Override
                    public void notifyDataChanged( String requestName, String requestMobile, String requestCarNumber ) {

                        //OrderFragment.this.requestName = requestName;
                        // OrderFragment.this.requestMobile = requestMobile;
                        //OrderFragment.this.requestCarNumber = requestCarNumber;
                    }
                } ).show( getSupportFragmentManager(), "choose" );
            }
        } );
    }

    public static class ChooseDialog extends BaseDialogFragment {

        private String requestName;
        private String requestMobile;
        private String requestCarNumber;

        private CallBack mCallBack;

        public interface CallBack {

            void notifyDataChanged( String requestName, String requestMobile, String requestCarNumber );
        }

        public static ChooseDialog newInstance( String name, String mobile, String carNumber, CallBack callBack ) {

            ChooseDialog fragment = new ChooseDialog();

            fragment.requestName = name;
            fragment.requestMobile = mobile;
            fragment.requestCarNumber = carNumber;
            fragment.mCallBack = callBack;

            return fragment;
        }

        @Override
        protected Dialog createDialog( Context context, @Nullable Bundle savedInstanceState ) {

            Dialog dialog = super.createDialog( context, savedInstanceState );
            View view = View.inflate( context, R.layout.order_choose_dialog, null );
            setDialogView( view );

            RecyclerView recyclerView = (RecyclerView) view.findViewById( R.id.recycler );
            recyclerView.setLayoutManager( new LinearLayoutManager( context ) );

            ArrayList< TypeViewHolder > list = new ArrayList<>();
            CommonLayout00 item0 = new CommonLayout00( "司机姓名", requestName ) {
                @Override
                protected void onViewCreate( View view ) {

                    super.onViewCreate( view );
                    //EditText editText = (EditText) view.findViewById( R.id.input );
                    //EditInputManager.setInputChineseName( editText );
                }
            };
            list.add( item0 );
            CommonLayout00 item1 = new CommonLayout00( "司机手机号", requestMobile ) {
                @Override
                protected void onViewCreate( View view ) {

                    super.onViewCreate( view );
                    //EditText editText = (EditText) view.findViewById( R.id.input );
                    //EditInputManager.setInputMobile( editText );
                }
            };
            list.add( item1 );
            CommonLayout00 item2 = new CommonLayout00( "车牌号", requestCarNumber ) {
                @Override
                protected void onViewCreate( View view ) {

                    super.onViewCreate( view );
                    //EditText editText = (EditText) view.findViewById( R.id.input );
                    //EditInputManager.setInput( editText, new CharTest() {
                    //    @Override
                    //    public boolean test( char c ) {
                    //
                    //        return EditInputManager.isPlateNo( String.valueOf( c ) );
                    //    }
                    //} );
                }
            };
            list.add( item2 );

            recyclerView.setAdapter( new MultiTypeAdapter( list ) );

            view.findViewById( R.id.reset ).setOnClickListener( new OnClickListener() {
                @Override
                public void onClick( View v ) {

                    requestName = "";
                    requestMobile = "";
                    requestCarNumber = "";

                    mCallBack.notifyDataChanged( requestName, requestMobile, requestCarNumber );

                    dismiss();
                }
            } );

            view.findViewById( R.id.confirm ).setOnClickListener( new OnClickListener() {
                @Override
                public void onClick( View v ) {

                    requestName = item0.getValue().toString();
                    requestMobile = item1.getValue().toString();
                    requestCarNumber = item2.getValue().toString();

                    mCallBack.notifyDataChanged( requestName, requestMobile, requestCarNumber );

                    dismiss();
                }
            } );

            return dialog;
        }
    }

    public static class CommonLayout00 extends BaseViewHolder {

        private final String key;
        private CharSequence value;

        public CommonLayout00( String key, String value ) {

            super( R.layout.common_layout_00 );
            this.key = key;
            this.value = value;
        }

        @Override
        protected void onViewCreate( View view ) {

            super.onViewCreate( view );

            View clear = view.findViewById( R.id.clear );
            EditText edit = view.findViewById( R.id.input );
            FocusTextWatcher.setTo( new ClearFocusTextWatcher( clear ) {

                @Override
                public void afterTextChanged( Editable s ) {

                    super.afterTextChanged( s );
                    value = s;
                    notifyValueChanged( value );
                }
            }, edit );
        }

        @Override
        protected void onBind( ViewHolder viewHolder, View root, int position, int type, List< Object > payloads ) {

            super.onBind( viewHolder, root, position, type, payloads );

            ((TextView) root.findViewById( R.id.text )).setText( key );
            EditText editText = (EditText) root.findViewById( R.id.input );
            editText.setHint( String.format( "请输入%s", key ) );
            editText.setText( value );
        }

        protected void notifyValueChanged( CharSequence newValue ) {

        }

        public CharSequence getValue() {

            return value;
        }
    }
}